package com.then.architecture.di;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.then.architecture.BuildConfigCallback;
import com.then.architecture.InjectorMap;

import javax.inject.Inject;

import dagger.android.AndroidInjection;
import dagger.android.HasActivityInjector;
import dagger.android.support.HasSupportFragmentInjector;
import timber.log.Timber;

/**
 * Created by 42524 on 2018/1/12.
 */

public class DaggerActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks {

    @Inject
    DaggerFragmentLifecycleCallbacks mFragmentLifecycleCallbacks;

    private InjectorMap injectorMap;

    @Inject
    public DaggerActivityLifecycleCallbacks(InjectorMap injectorMap) {
        this.injectorMap = injectorMap;
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        Timber.w(activity + " ---> onActivityCreated");
        if (activity instanceof BuildConfigCallback) {
            BuildConfigCallback callback = (BuildConfigCallback) activity;
            String buildConfigId = callback.buildConfigId();
            HasActivityInjector hasActivityInjector=  injectorMap.getActivityInjector(buildConfigId);
            hasActivityInjector.activityInjector().inject(activity);
            if ((activity instanceof HasSupportFragmentInjector || activity.getApplication() instanceof HasSupportFragmentInjector)
                    && activity instanceof FragmentActivity) {
                ((FragmentActivity) activity).getSupportFragmentManager()
                        .registerFragmentLifecycleCallbacks(mFragmentLifecycleCallbacks, true);
            }
        }
    }

    @Override
    public void onActivityStarted(Activity activity) {

    }

    @Override
    public void onActivityResumed(Activity activity) {

    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        Timber.w(activity + " ---> onActivityDestroyed");
    }
}
